package org.medicalinstrument.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
import org.medicalinstrument.bean.BuyInfo;
import org.medicalinstrument.bean.Customer;
import org.medicalinstrument.bean.CustomerAddress;
import org.medicalinstrument.service.CustomerService;


import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName CustomerAction
 * @Description TODO
 * @AUTHOR cirly
 * @DaTe 2018/11/3 20:58
 * @Modify by
 * @Version 1.0
 **/
public class CustomerAction extends ActionSupport {
    private String custNo;
    private String custPhone;
    private String custName;
    private String gender;
    private String custAddr;
    private String custAge;
    private String connect;
    private String pwd;
    private String accountNo;
    private String password;
    private String age;
    private String sex;
    private String consigeeAddr;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getConsigeeAddr() {
        return consigeeAddr;
    }

    public void setConsigeeAddr(String consigeeAddr) {
        this.consigeeAddr = consigeeAddr;
    }

    private String address;
    private String productQR;
    private String orderId;

    public String getProductQR() {
        return productQR;
    }

    public void setProductQR(String productQR) {
        this.productQR = productQR;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public String getCustNo() {
        return custNo;
    }

    public void setCustNo(String custNo) {
        this.custNo = custNo;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getCustAddr() {
        return custAddr;
    }

    public void setCustAddr(String custAddr) {
        this.custAddr = custAddr;
    }

    public String getCustAge() {
        return custAge;
    }

    public void setCustAge(String custAge) {
        this.custAge = custAge;
    }

    public String getConnect() {
        return connect;
    }

    public void setConnect(String connect) {
        this.connect = connect;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    private String pass;
    private String returnString;

    public String getReturnString() {
        return returnString;
    }

    public void setReturnString(String returnString) {
        this.returnString = returnString;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
    CustomerService cs = new CustomerService();

    public String CustomerLogin () throws SQLException {
        System.out.println(accountNo);
        returnString = cs.CustomerLogin(accountNo,pass);

        if(!returnString.equals("loginfalse")){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }

    public String isExistRe () throws SQLException {
        System.out.println(accountNo);
        returnString = cs.isExist(accountNo);

        System.out.println(returnString);
        if(returnString.equals("notexist") ){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }

    public String register () throws SQLException {

        returnString = cs.register(custPhone,custName,gender,custAddr,Integer.parseInt(custAge),connect,pwd,custNo);
        if(returnString.equals("success") ){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }

   public String getAddress () throws SQLException {
        returnString = cs.getAddress(accountNo);
       System.out.println(returnString);
        if ("address_null".equals(returnString)){
            return ERROR;
        }else{
            return SUCCESS;
        }
   }

   public String addAddress() throws SQLException {
      // CustomerAddress customerAddress = (CustomerAddress) JSON.parseArray(address, CustomerAddress.class);
      // System.out.println(customerAddress);
//       List list = JSON.parseArray(address, CustomerAddress.class);
//       System.out.println(list.get(0));
       CustomerAddress customerAddress =  JSON.parseObject(address,CustomerAddress.class);
     //
       //System.out.println(customerAddress);
       returnString = cs.addAddress(customerAddress);
        return SUCCESS;
   }
    public String getAllCustomerList() throws SQLException {
        returnString = cs.getAllCustomerList();
        System.out.println(returnString);
        if (returnString.equals("nocustomer")) {
            return ERROR;
        } else {
            return SUCCESS;
        }
    }
    public String Search() throws SQLException {
        returnString = cs.Search(accountNo,custPhone);
        System.out.println(returnString);
        if (returnString.equals("nocustomer")) {
            return ERROR;
        } else {
            return SUCCESS;
        }
    }
    public String modifycustomer() throws SQLException {
        returnString = cs.modifycustomer(custPhone,accountNo,password,consigeeAddr,custName,sex,age);
        System.out.println(returnString);
        if (returnString.equals("nocustomer")) {
            return ERROR;
        } else {
            return SUCCESS;
        }
    }

    public String delCustomer(){
        System.out.println("客户账号："+accountNo);
        returnString = cs.removeCustomer(accountNo);
        return SUCCESS;
    }


    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String deleteOrder()throws SQLException{
        //System.out.println(orderId+" "+accountNo+" "+productQR);
        returnString = cs.deleteOrder(orderId,accountNo,productQR);
        if (returnString.equals("delete_success")){
            return SUCCESS;
        }else{
            return ERROR;
        }
        //return SUCCESS;
   }
}
